/**
 * IOUtils.java  [V 1.0.0]
 * classpath : com.zyw.utils.IOUtils
 * zhangyw Create at 2014年8月11日 下午3:24:35
 */
package com.zyw.utils;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Environment;
import android.support.v4.os.EnvironmentCompat;

/**
 * Android的一些常用IO方法 这里的IO通用方法是想写作给以后的工作提供方便
 * 
 * @author zhangyw create at 2014年8月11日 下午3:24:35
 */
public class IOUtils {
  private static final String TAG = "IOUtils";

  /**
   * @author zhangyw
   * @return 返回DICM的DIR的FILE对象，失败则返回null
   */
  public static File getDICMDir() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
      return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    }
    return null;
  }
  
  @SuppressLint("NewApi")
  public static File getExteralStorageDir(){
    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
      return Environment.getExternalStorageDirectory();
    }else if(VERSION.SDK_INT>=9&&!Environment.isExternalStorageRemovable()){
      return Environment.getExternalStorageDirectory();
    }
    return null;
  }
  

  /**
   * 根据filter，返回指定类型的file列表，包括子文件夹
   * @author zhangyw
   * @param Dirname
   * @param filter
   * @return
   */
  public static List<File>getFilesFromDir(File Dirname,FileFilter filter){
    if (!Dirname.isDirectory()||Dirname.isHidden()) {
      //我们不处理隐藏文件的内容
      return null;
    }
    List<File> rst = null;
    File[] fileList = Dirname.listFiles();
    rst = new ArrayList<File>(fileList.length);
    for (File tmp : fileList) {
      if (tmp.isDirectory()) {
        List<File> collection = getFilesFromDir(tmp,filter);
        if (collection != null) {
          rst.addAll(collection);
        }
      } else if (filter.accept(tmp)) {
        rst.add(tmp);
      }
    }
    return rst;
  
  }
  
  
  public static List<File> getPicsFromDir(File Dirname) {
    FileFilter filter = new FileFilter() {
      @Override
      public boolean accept(File pathname) {
        return isPic(pathname.getName());
      }
    };
    return getFilesFromDir(Dirname, filter);
  }

  /*aduio format*/
  public static boolean isOgg(String filename){
    return filename.endsWith(".ogg")||filename.endsWith(".OGG");
  }
  public static boolean isMp3(String filename){
    return filename.endsWith(".mp3")||filename.endsWith(".MP3");
  }
  public static boolean isAac(String filename){
    return filename.endsWith(".aac")||filename.endsWith(".AAC");
  }
  public static boolean isWAV(String filename){
    return filename.endsWith(".wav")||filename.endsWith(".WAV");
  }
  
  public static boolean isAduio(String filename){
    return isOgg(filename)||isMp3(filename)||isAac(filename)||isWAV(filename);
  }
  
  
  /*image format*/
  public static boolean isJpg(String filename) {
    return filename.endsWith(".jpg") || filename.endsWith(".JPG");
  }

  public static boolean isPng(String filename) {
    return filename.endsWith(".png") || filename.endsWith(".PNG");
  }

  public static boolean isGif(String filename) {
    return filename.endsWith(".gif") || filename.endsWith(".GIF");
  }

  public static boolean isPic(String filename) {
    return isGif(filename) || isJpg(filename) || isPng(filename);
  }

  /**
   * @author zhangyw
   * @param context
   * @param uniqueName
   *          could be null,if dont need any
   * @return
   */
  @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  public static File getPrivateCacheDir(Context context, String uniqueName) {
    File cachePath = (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment
        .isExternalStorageRemovable()) ? context.getExternalCacheDir() : context.getCacheDir();
    if (uniqueName != null) {
      File cacheDirWithName = new File(cachePath, uniqueName);
      if (!cacheDirWithName.isDirectory()) {
        cacheDirWithName.mkdir();
      }
      return cacheDirWithName;
    }
    return cachePath;
  }

}
